/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: AbstractParams.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.network.params;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.CacheControl;
import okhttp3.Headers;


public abstract class AbstractParams implements IParams {
    protected String url;

    protected Object tag;

    protected CacheControl cacheControl;

    protected long requestTime;

    protected Map<String, String> headers = new HashMap<>();

    public AbstractParams(Builder builder) {
        if (builder.baseParams != null) {
            url = builder.baseParams.url;
            tag = builder.baseParams.tag;
        }

        if (!TextUtils.isEmpty(builder.url))
            url = builder.url;

        if (tag != null)
            tag = builder.tag;

        //requestTime = System.currentTimeMillis();
        this.cacheControl = builder.cacheControl;
        this.headers = builder.headers;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    @Override
    public String getUrl() {
        return url;
    }

    @Override
    public Object getTag() {
        return tag;
    }

    @Override
    public long getRequestTime() {
        return requestTime;
    }

    @Override
    public void setRequestTime(long time) {
         requestTime = time;
    }

    @Override
    public CacheControl getCacheControl() {
        return cacheControl;
    }

    public static abstract class Builder<B extends Builder, T extends AbstractParams> {
        public T baseParams;

        protected String url;

        protected Object tag;

        protected CacheControl cacheControl;

        protected Map<String, String> headers = new HashMap<>();

        public B base(T params) {
            this.baseParams = params;
            return (B) this;
        }

        public B url(String url) {
            this.url = url;
            return (B) this;
        }

        public B tag(Object tag) {
            this.tag = tag;
            return (B) this;
        }

        public abstract B addParam(String key, Object value);

        public B cacheControl(CacheControl cacheControl) {
            this.cacheControl = cacheControl;
            return (B) this;
        }

        public void addHeader(String key, String value){
            headers.put(key, value);
        }

        public abstract T build();
    }
}
